<!-- 功能：设置系统时间弹窗 | 作者：menghao.yan | 邮箱：yxs2615@163.com | 时间：2023年08月24日 14:23:00 -->
<template>
  <!-- 弹出框 -->
  <a-modal v-model:open="visible" title="设置系统时间" width="800px" class="y-modal" okText="创建" @ok="onSubmit" @cancel="onCancel">
    <a-row>
      <a-col :span="24">
        <a-alert banner message="说明:  时间和日期配置选项让您可以对内部系统时钟进行配置，更新和维护。在该选项中您可以设置您所在的时区和NTP（网络时间协议）服务器，也可以手动设置日期和时间。修改时间后您需要重新登录系统！" type="warning" show-icon />
      </a-col>
    </a-row>
    <a-form :label-col="{ span: 8 }" :wrapper-col="{span: 12}">
      <a-divider orientation="left">时间和日期设置</a-divider>
      <a-form-item label="当前时间">
        {{ currentData.currdatetime || '暂无' }}
      </a-form-item>
      <a-form-item label="当前时区">
        {{currentData.currtimezone || '暂无'}}
      </a-form-item>
      <a-form-item label="时区" v-bind="validateInfos.timezone">
        <a-select v-model:value="modelRef.timezone" placeholder="不更改时区">
          <a-select-option :value="item[0]" v-for="item in timezones">{{item[0]}}</a-select-option>
        </a-select>
      </a-form-item>
      <a-form-item label="设置时间" v-bind="validateInfos.time">
        <a-date-picker show-time v-model:value="modelRef.time" placeholder="请选择时间" style="width:100%" valueFormat="YYYY-MM-DD HH:mm:ss" />
      </a-form-item>
      <a-divider orientation="left">自动时间和日期配置</a-divider>
      <a-form-item label="自动与时间服务器同步">
        <a-checkbox v-model:checked="modelRef.tongBu" />
      </a-form-item>
      <a-form-item label="时间服务器">
        {{modelRef.timeserver}}
      </a-form-item>
    </a-form>
  </a-modal>
</template>

<script setup>
const props = defineProps({
  visible: {
    type: Boolean,
    default: false
  }
})

const { visible } = toRefs(props)

// 定义组件的事件
const emits = defineEmits(['update:visible'])

import { Form } from 'ant-design-vue'

const useForm = Form.useForm

const modelRef = reactive({
  timezone: '',
  time: '',
  timeserver: '',
  tongBu: false
})

const rulesRef = reactive({
  timezone: [
    {
      required: true,
      message: '请选择时区'
    }
  ],
  time: [
    {
      required: true,
      message: '请选择时间'
    }
  ]
})

const { resetFields, validate, validateInfos, mergeValidateInfo } = useForm(modelRef, rulesRef)
const onSubmit = () => {
  validate()
    .then(() => {
      console.log('设置系统时间>>', toRaw(modelRef))
      // 设置系统时间
      setSystemtoolSettime()
      onCancel()
    })
    .catch(err => {
      console.log('error', err)
    })
}

const onCancel = () => {
  // 触发自定义事件来通知父组件关闭弹窗
  emits('update:visible', false)
}

const getNowData = () => {
  let now = new Date()
  let year = now.getFullYear()
  let month = now.getMonth() + 1
  let day = now.getDate()
  let hour = now.getHours()
  let minute = now.getMinutes()
  let second = now.getSeconds()
  return `${year}-${month}-${day} ${hour}:${minute}:${second}`
}

// visible 改变的时候
watch(
  () => props.visible,
  val => {
    if (val) {
      initSystemtoolGettime() // 获取系统时间
    }
  }
)

// 时区列表选项
let timezones = ref([
  ['Asia/Shanghai', ''],
  ['Asia/Shanghai2', '']
])
// 当前时间 和 时区
const currentData = reactive({
  currdatetime: '',
  currtimezone: ''
})
// 获取系统时间
const initSystemtoolGettime = index => {
  apiWs.systemtoolGettime().subscribe(resp => {
    // 服务器当前时间
    modelRef.time = resp.currdatetime
    currentData.currdatetime = resp.currdatetime
    // 当前时区
    modelRef.timezone = resp.currtimezone
    currentData.currtimezone = resp.currtimezone
    // 时区列表选项
    timezones.value = resp.timezones
    // 时区服务器
    modelRef.timeserver = resp.timeserver
  })
}

// 设置系统时间
const setSystemtoolSettime = () => {
  let params = {
    autosync: modelRef.tongBu,
    timeserver: modelRef.timeserver,
    timezone: modelRef.timezone,
    timestr: modelRef.time
  }
  apiWs.systemtoolSettime(params).subscribe(resp => {
    // 服务器当前时间
    modelRef.time = resp.currdatetime
    currentData.currdatetime = resp.currdatetime
    // 当前时区
    modelRef.timezone = resp.timezone
    currentData.currtimezone = resp.timezone
    // 时区列表选项
    timezones.value = resp.timezones
  })
}
</script>

<style scoped>
.ant-row {
  margin-bottom: 10px;
}
</style>